package com.ds.smartstudio.mapper;

import com.ds.smartstudio.controller.DTO.deviceObj;
import com.ds.smartstudio.entity.Device;
import com.ds.smartstudio.entity.History;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author yangsf
 * @since 2022-07-02 10:42:24
 */
@Mapper
public interface HistoryMapper extends BaseMapper<History> {
    @Select("SELECT * FROM ( SELECT ROW_NUMBER () OVER ( PARTITION BY ds_device_mac ORDER BY create_time DESC ) AS rowNum ,* FROM ds_history) d WHERE rowNum = 1 and ds_device_mac = #{mac}")
    History selectLatestByMac(String mac);
    @Select("select count(*) from ds_history where ds_device_mac like CONCAT('%',#{name},'%')")
    Integer selectTotalByMac(String name);

    @Select("SELECT ds_device_mac FROM ds_device WHERE ds_area_id=(SELECT ds_area_id FROM ds_area WHERE ds_area_name = #{area})")
    List<String> selectMacByArea(String area);

    @Select("SELECT ds_type_id FROM ds_type WHERE ds_type_name=#{Typename}")
    Integer selectTypeIdByTypename(String Typename);

    @Select("SELECT ds_device_mac FROM ds_device WHERE ds_type_id=#{TypeId}")
    List<String> selectDeviceMacByTypeId(int TypeId);

    @Select("SELECT * FROM ds_device")
    List<Device> selectAllType();


    @Select("SELECT ds_area_id FROM ds_area WHERE ds_area_name =#{Areaname}")
    Integer selectAreaIdByAreaname(String Areaname);

    List<History> HistoryQuerry(Integer area,String mac,Timestamp timeStart,Timestamp timeEnd);

    @Select("SELECT * FROM ( SELECT ROW_NUMBER () OVER ( PARTITION BY ds_device_mac ORDER BY create_time DESC ) AS rowNum ,* FROM ds_history) d WHERE rowNum = 1")
    List<History> selectLatest();
}
